package com.mobilesales.android.activities.employees;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.mobilesales.android.R;
import com.mobilesales.android.activities.ModelActivity;
import com.mobilesales.android.adapters.SlidingMenus;
import com.mobilesales.android.database.EmployeeManager;
import com.mobilesales.android.model.employees.Employee;
import com.mobilesales.android.model.employees.Message;
import com.mobilesales.android.model.exceptions.ServiceUnavailableException;
import com.mobilesales.android.services.EmployeeServiceClient;
import com.mobilesales.android.util.Constants;
import com.mobilesales.android.util.LogFlags;
import com.mobilesales.android.util.MessageBox;
import com.mobilesales.android.util.Settings;

public class MessageRead extends ModelActivity<Message> {

	private int messageId;
	private Message message;
	private Employee sender;

	private TextView senderField;
	private TextView sendDateField;
	private TextView subjectField;
	private TextView contentField;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.message_read);
		new SlidingMenus(this);
		senderField = (TextView) this.findViewById(R.id.senderTxt);
		sendDateField = (TextView) this.findViewById(R.id.sentDateTxt);
		subjectField = (TextView) this.findViewById(R.id.subjectTxt);
		contentField = (TextView) this.findViewById(R.id.contentTxt);

		Bundle bundle = this.getIntent().getExtras();
		if (bundle != null && bundle.containsKey(Constants.MessageId)) {
			messageId = bundle.getInt(Constants.MessageId);
			loadItem(messageId);
		} else {
			MessageBox.show(this, getString(R.string.ErrorMessage));
		}
	}

	@Override
	protected void setModel(Message model) {
		super.setModel(model);
		String senderName = model.SenderLogin;
		if (sender != null)
			senderName = String.format("%s %s", sender.Lastname,
					sender.Firstname);

		senderField.setText(senderName);
		sendDateField.setText(model.SendDate);
		subjectField.setText(model.Subject);
		contentField.setText(model.Content);
	}

	private void loadItem(int messageId) {
		message = getMessage(messageId);
		if (message != null) {
			sender = this.getEmployee(message.SenderId);
			if (sender == null)
				MessageBox.show(this, getString(R.string.ErrorEmployee));

			this.setModel(message);
		} else {
			MessageBox.show(this, getString(R.string.ErrorMessage));
		}
	}

	private Message getMessage(int messageId) {
		EmployeeManager manager = new EmployeeManager(this);
		Message message = manager.GetMessage(messageId);
		if (message != null) {
			if (message.IsNew || changeReadStatus(messageId)) {
				message.IsNew = false;
				manager.StoreMessage(message);
			}
		}
		manager.Close();
		return message;
	}

	private boolean changeReadStatus(int messageId) {
		try {
			EmployeeServiceClient cli = new EmployeeServiceClient(
					Settings.EmployeeService());
			cli.readMessage(messageId);
			return true;
		} catch (ServiceUnavailableException e) {
			Log.e(LogFlags.RestService, e.getMessage(), e);
		}
		return false;
	}

	private Employee getEmployee(int employeeId) {
		EmployeeManager manager = new EmployeeManager(this);
		Employee e = manager.GetEmployee(employeeId);
		manager.Close();
		return e;
	}

	public void refresh() {
		this.getHandler().post(new Runnable() {
			public void run() {
				loadItem(messageId);
			}
		});
	}

	public void finishs(View v) {
		setResult(Activity.RESULT_CANCELED);
		finish();
	}

	@Override
	public void onBackPressed() {
		finish();
	}
}
